<?php
session_start();
date_default_timezone_set("PRC");
$conn = new mysqli("localhost", "root", "", "simple_weibo");
$conn->query("set names 'utf8'");

// 信息窗口
function show_msg($msg, $url)
{
    $win_str = "<meta http-equiv='refresh' content='3; url=" . $url . "' />
    <table width=200 border=1 align=center>
    <tr>
        <td align=center><strong>提示窗口</strong></td>
    </tr>
    <tr>
        <td align=center><p>" . $msg . "</p>
            <p>3秒后自动跳转！<a href='" . $url . "'>跳转</a></p>
        </td>
    </tr>
</table>";
    echo $win_str;
    exit();
}

// 注册
function register($user, $pw, $tele)
{
    // 连接数据库服务器
    global $conn;
    $sql = "insert into weibo_user(user_name,user_pw,user_tx,telephone) values('$user','$pw','img/person.jpg','$tele')";
    $res = $conn->query($sql);
    if ($res) {
        show_msg("注册成功!", "login.php");
    } else {
        show_msg("注册失败!", "register.php");
    }
}

// 登录
// 登录功能
function login($user, $pw)
{
    global $conn;
    $sql = "select * from weibo_user where user_name='$user' and user_pw='$pw'";
    $res = $conn->query($sql);
    if ($res->num_rows > 0) {
        $row = $res->fetch_assoc();
        $_SESSION['user'] = $row['user_name'];
        show_msg("欢迎" . $_SESSION['user'] . "访问!", "index_loginafter.php");
    } else {
        show_msg("用户名或密码错误!", "login.php");
    }
}

// 找回密码
function findpassword($user, $pw, $tele){
    global $conn;
    $sql = "select * from weibo_user where user_name='$user' and telephone = '$tele'";
    $res = $conn->query($sql);
    if ($res->num_rows > 0) {
        $sql = "update weibo_user set user_pw = '$pw' where user_name='$user' and telephone = '$tele'";
        $res = $conn->query($sql);
        if ($res) {
            show_msg("找回成功", "login.php");
        } else {
            show_msg("找回失败", "findpw.php");
        }
    } else {
        show_msg("用户名或手机号错误!", "findpw.php");
    }
}

//退出登录
function logout(){
    session_destroy();
    show_msg("退出登录","index.php");
}

// 发表评论
function comment($author, $content, $time, $id){
    global $conn;
    
    $result = array();
    preg_match("/(?:\@)(.*)(?:\s)/", $content, $result);
    
    if($result != null){
        $sql = "select * from weibo_user where user_name='".$result[1]."'";
        $res = $conn->query($sql);
        if($res->num_rows){
            $row = $res->fetch_assoc();
            $content=str_replace($result[0], '<a href="person.php?id='.$row['user_id'].'">'.$result[0].'</a>', $content);
        }
    }
    
    $sql = "insert into weibo_comment (cm_author,  cm_content, cm_time, weibo_id) values ('".$author."', '".$content."', '".$time."', '".$id."')";
    $res = $conn->query($sql);
    if($res){
        $cmsql = "update weibo_list set weibo_cm=weibo_cm+1, weibo_hot=weibo_hot+1 where weibo_id='".$id."'";
        $cmres = $conn->query($cmsql);
        show_msg("发表评论成功", "detail_loginafter.php?id=".$id);
    }else{
        show_msg("发表评论失败", "detail_loginafter.php?id=".$id);
    }
}

// 转发weibo
function relweibo($author, $time, $content, $id){
    global $conn;
    
    $result = array();
    preg_match("/(?:\@)(.*)(?:\s)/", $content, $result);
    
    if($result != null){
        $sql = "select * from weibo_user where user_name='".$result[1]."'";
        $res = $conn->query($sql);
        if($res->num_rows){
            $row = $res->fetch_assoc();
            $content=str_replace($result[0], '<a href="person.php?id='.$row['user_id'].'">'.$result[0].'</a>', $content);
        }
    }
    
    $sql = "insert into weibo_list (author, public_time, weibo_content, relwb_id) values ('".$author."', '".$time."', '".$content."', '".$id."')";
    $res = $conn->query($sql);
    if($res){
        $relsql = "update weibo_list set weibo_rel=weibo_rel+1, weibo_hot=weibo_hot+1 where weibo_id='".$id."'";
        $relres = $conn->query($relsql);
        show_msg("转发微博成功", "detail_loginafter.php?id=".$id);
    }else {
        show_msg("转发微博失败", "detail_loginafter.php?id=".$id);
    }
}

// 更改密码
function fixpw($user, $tele, $pw, $new){
    global $conn;
    $searchsql = "select * from weibo_user where user_name='".$user."' and telephone='".$tele."' and user_tx='".$pw."'";
    $searchres = $conn->query($searchsql);
    $searnum = $searchres->num_rows;
    if($searnum){
        $sql = "update weibo_user set user_pw='".$new."' where user_name='".$user."'";
        $res = $conn->query($sql);
        if($res){
            show_msg("密码修改成功", "index_loginafter.php");
        }else {
            show_msg("密码修改失败", "fixpw.php");
        }
    }else {
        show_msg("手机号码或原密码输错", 'fixpw.php');
    }
}

// 发布微博
function wbpublic($file, $tmpName, $user, $time, $content){
    global $conn;
    
    $result = array();
    preg_match("/(?:\@)(.*)(?:\s)/", $content, $result);
    
    if($result != null){
        $sql = "select * from weibo_user where user_name='".$result[1]."'";
        $res = $conn->query($sql);
        if($res->num_rows){
            $row = $res->fetch_assoc();
            $content=str_replace($result[0], '<a href="person.php?id='.$row['user_id'].'">'.$result[0].'</a>', $content);
        }
    }
    
    if (isset($file) && is_uploaded_file($tmpName)){
        $imgFile = $file;
        $upErr = $imgFile['error'];
        if ($upErr == 0){
            $imgType = $imgFile['type']; //文件类型。
            /* 判断文件类型，这个例子里仅支持jpg和gif类型的图片文件。*/
            if ($imgType == 'image/jpeg'|| $imgType == 'image/gif'){
                
                //通过反转及分割获取图片的后缀
                $string = strrev($imgFile['name']);
                $array = explode('.',$string);
                $ex = strrev($array[0]);
                
                //将上传图片重命名为日期加100-999的随机数并加上后缀名
                $imgFile['name'] = date("Y").date("m").date("d").date("H").date("i").date("s").rand(100, 999).".".$ex;
                
                $imgFileName = $imgFile['name'];
                $imgSize = $imgFile['size'];
                $imgTmpFile = $imgFile['tmp_name'];
                /* 将文件从临时文件夹移到上传文件夹中。*/
                move_uploaded_file($imgTmpFile, '../img/'.$imgFileName);
                
                $imgPath="img/".$imgFile['name'];
                $sql = "insert into weibo_list(author, public_time, weibo_img, weibo_content) values
('".$user."','".$time."', '".$imgPath."', '".$content."')";
                $res = $conn->query($sql);
                if($res){
                    show_msg("发布成功", "index_loginafter.php");
                }else {
                    show_msg("发布失败", "index_loginafter.php");
                }
                
            }else{
                echo "请选择jpg或gif文件，不支持其它类型的文件。";
            }
        }else{
            echo "文件上传失败。<br>";
            switch ($upErr){
                case 1:
                    echo "超过了php.ini中设置的上传文件大小。";
                    break;
                case 2:
                    echo "超过了MAX_FILE_SIZE选项指定的文件大小。";
                    break;
                case 3:
                    echo "文件只有部分被上传。";
                    break;
                case 4:
                    echo "文件未被上传。";
                    break;
                case 5:
                    echo "上传文件大小为0";
                    break;
            }
        }
    }else{
        $sql = "insert into weibo_list(author, public_time, weibo_content) values
('".$user."','".$time."', '".$content."')";
        $res = $conn->query($sql);
        if($res){
            show_msg("发布成功", "index_loginafter.php");
        }else {
            show_msg("发布失败", "index_loginafter.php");
        }
    }
}

// 添加子评论
function addkidcm($txt, $time, $id, $wid){
    global $conn;
    $result = array();
    preg_match("/(?:\@)(.*)(?:\s)/", $txt, $result);
    
    if($result != null){
        $sql = "select * from weibo_user where user_name='".$result[1]."'";
        $res = $conn->query($sql);
        if($res->num_rows){
            $row = $res->fetch_assoc();
            $txt=str_replace($result[0], '<a href="person.php?id='.$row['user_id'].'">'.$result[0].'</a>', $txt);
        }
    }
    
    $addsql="insert into weibo_kidcomment (kidcm_author, kidcm_content, kidcm_time, cm_id, weibo_id) values ('".$_SESSION['user']."', '".$txt."', '".$time."', '".$id."', '".$wid."')";
    $addres=$conn->query($addsql);
    if($addres){
        show_msg("回复成功", "kid_comment.php?id=".$id);
    }else {
        show_msg("回复失败", "kid_comment.php?id=".$id);
    }
        
    
}